package net.thetabx.ftdba;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.thetabx.ftdba.http.HttpData;
import net.thetabx.ftdba.http.HttpManager;
import net.thetabx.ftdba.objects.User;


public class Userlist {
	private RStatus status = RStatus.OK;
	private String pureData;
	private List<User> userList;
	private List<User> userListIn;
	private List<User> userListOut;
	
	public void pullUsers(HttpManager http) {
		HttpData response = http.send("/?section=COMMUNAUTE&module=mod_shoutbox&ajax=1&mod_ajax=1&uonline=1");
		this.processData(response);
	}
		
	private void processData(HttpData data) {
		if(data.getStatus() != RStatus.ENDED) {
			status = data.getStatus();
			return;
		}
		
		StringBuilder content = data.getContentSb();
		this.pureData = new String(content);
		
		status = RStatus.ANALYZING;
		List<User> tempUserList = new ArrayList<User>(); 
		Pattern p = Pattern.compile("<a href=\"/\\?section=ACCOUNT_INFOS&hash=(\\w+)\" class=\"class_(\\d+) can_leech_[0-1]\">(\\w+)</a>");
		Matcher m = p.matcher(content);
		while(m.find()) {
			tempUserList.add(new User(m.group(1), m.group(2), m.group(3)));
		}
		
		status = RStatus.COMPARISON;
		if(userList == null) {
			userListIn = new ArrayList<User>();
			userListOut = new ArrayList<User>();
		}
		else {
			
		}
		
		userList = tempUserList;
		status = RStatus.ENDED;
	}
	
	public RStatus getStatus() {
		return status;
	}
	
	public String getPureData() {
		return pureData;
	}
	
	public List<User> getUserList() {
		return userList;
	}

	public List<User> getUserListIn() {
		return userListIn;
	}

	public List<User> getUserListOut() {
		return userListOut;
	}
}
